<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="120px"
    >
      <el-form-item label="平台总分账编号" prop="createTime">
        <el-input v-model="queryParams.id" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="付款状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MEIJIA_DIVIDE_RESULT_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button @click="handleEditDrawer"
          ><Icon icon="ep:search" class="mr-5px" /> 编辑</el-button
        >
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="平台总分账编号" align="center" prop="divideAllCode" />
      <el-table-column label="收款人部门" align="center" prop="deptName" />
      <el-table-column label="收款人" align="center" prop="name" />
      <el-table-column label="关联订单" align="center" prop="orderCount">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openOrderList(scope.row.divideAllCode, scope.row.userCode)"
          >
            {{ scope.row.orderCount }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="dividePriceCNY" />
      <el-table-column label="银行" align="center" prop="bankName" />
      <el-table-column label="银行卡号" align="center" prop="card" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="付款状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MEIJIA_DIVIDE_RESULT_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="分账时间" align="center" :formatter="dateFormatter" width="200px">
        <template #default="scope">
          {{ formatDate(scope.row.divideTimeBegin, 'YYYY/MM/DD') }}-{{
            formatDate(scope.row.divideTimeEnd, 'YYYY/MM/DD')
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="180px"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <DivideSelfOrderDialog ref="divideOrder" />
</template>

<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
import { dateFormatter, formatDate } from '@/utils/formatTime'
import DivideSelfOrderDialog from './DivideSelfOrderDialog.vue'
import * as DivideApi from '@/api/meijia/divide'
defineOptions({ name: 'DivideResultSelf' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const divideEdit = ref()
const divideConfirm = ref()
const divideOrder = ref()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DivideApi.getSelfDivideResultPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
const handleEditDrawer = () => {
  divideEdit.value.open()
}
const handleConfirmDrawer = () => {
  divideConfirm.value.open()
}
const handleOrder = () => {
  divideOrder.value.open()
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
const openOrderList = (code: string, userCode: string) => {
  divideOrder.value.open(code, userCode)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
